package cn.kgc.springcloud;

import com.google.common.collect.ImmutableMap;

import lombok.Getter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;

import java.time.Duration;
import java.util.Map;

@EnableCircuitBreaker
@EnableEurekaClient
@EnableCaching
@SpringBootApplication
public class faqssearch {
    public static void main(String[] args) {
        SpringApplication.run(faqssearch.class,args);
    }

    @Bean
    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
        return (builder) -> {
            for (Map.Entry<String, Duration> entry : RedisCacheName.getCacheMap().entrySet()) {
                builder.withCacheConfiguration(entry.getKey(),
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(entry.getValue()));
            }
        };
    }

    public static class RedisCacheName {
        public static final String cache_10SEC = "CACHE_10SEC";
        @Getter
        private static final Map<String, Duration> cacheMap;
        static {
            cacheMap = ImmutableMap.<String, Duration>builder().put(cache_10SEC, Duration.ofMinutes(10L)).build();
        }
    }

}
